package com.foreknow.hospital.controller;

import com.foreknow.hospital.po.Orders;
import com.foreknow.hospital.service.OrdersService;
import com.foreknow.hospital.service.impl.OrdersServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class OrdersController {
    /**
     * HttpServletRequest
     * 提供一些方法用于获取前台传递到后台的请求参数
     * 客户端向服务端发送请求的url地址
     * http://localhost:8082/tijian/OrdersController/getOrdersByUserId?userId=12345671111
     * @return
     */
    public Object getOrdersByUserId(HttpServletRequest request) throws SQLException {
        //获取前台的请求参数
        String userId= request.getParameter("userId");
        Orders orders=new Orders();
        orders.setUserId(userId);
        //调用Service中的方法
        OrdersService ordersService = new OrdersServiceImpl();
        int state=ordersService.getOrdersByUserId(orders);

        return state;
    }
    /**
     * HttpServletRequest
     * 提供一些方法用于获取前台传递到后台的请求参数
     * 客户端向服务端发送请求的url地址
     * http://localhost:8082/tijian/OrdersController/saveOrders?
     * @return
     */
    public int saveOrders(HttpServletRequest request) throws SQLException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        //String currentDate=sdf.format(new Date());
        Orders orders=new Orders();
        orders.setUserId(request.getParameter("userId"));
       // orders.setOrderId(Integer.parseInt(request.getParameter("orderId")));
        orders.setOrderDate(request.getParameter("orderDate"));
        orders.setHpId(Integer.parseInt(request.getParameter("hpId")));
        orders.setSmId(Integer.parseInt(request.getParameter("smId")));
        OrdersService ordersService = new OrdersServiceImpl();
        int result=ordersService.saveOrders(orders);
        return result;
    }
    /**
     * HttpServletRequest
     * 提供一些方法用于获取前台传递到后台的请求参数
     * 客户端向服务端发送请求的url地址
     * http://localhost:8082/tijian/OrdersController/listOrdersByUserId?userId=12345671111
     * @return
     */
    public Object listOrdersByUserId(HttpServletRequest request) throws SQLException {
        //获取前台的请求参数
        String userId= request.getParameter("userId");
       // int state=Integer.parseInt(request.getParameter("state"));
        Orders orders=new Orders();
        orders.setUserId(userId);
        orders.setState(Integer.parseInt(request.getParameter("state")));
        //调用Service中的方法
        OrdersService ordersService = new OrdersServiceImpl();
        List<Orders> res=ordersService.listOrdersByUserId(orders);
        return res;
    }
    /**
     * HttpServletRequest
     * 提供一些方法用于获取前台传递到后台的请求参数
     * 客户端向服务端发送请求的url地址
     * http://localhost:8082/tijian/OrdersController/removeOrders
     * @return
     */
    public Object removeOrders (HttpServletRequest request) throws SQLException {
        int res=0;
        //获取前台的请求参数
       int orderId=Integer.parseInt(request.getParameter("orderId"));

        Orders orders=new Orders();
        orders.setOrderId(orderId);
        //调用Service中的方法
        OrdersService ordersService = new OrdersServiceImpl();
        res=ordersService.removeOrders(orders);
        return res;
    }
    /**
     * HttpServletRequest
     * 提供一些方法用于获取前台传递到后台的请求参数
     * 客户端向服务端发送请求的url地址
     * http://localhost:8082/tijian/OrdersController/getOrdersById
     * @return
     */
    public Object getOrdersById (HttpServletRequest request) throws SQLException {
        int res=0;
        //获取前台的请求参数
        String orderId=request.getParameter("orderId");
        Orders orders=new Orders();
        orders.setOrderId(Integer.parseInt(orderId));
        //调用Service中的方法
        OrdersService ordersService = new OrdersServiceImpl();
        Orders result=ordersService.getOrdersById(orders);
        return result;
    }
}


